<?php
namespace app\index\controller;
use think\Db;
use think\Request;
use think\Controller;

class Notice extends Base
{
    // 新增修改公告
    public function noticeAdd()
    {
        $data = input();//token id name标题 zhiding置顶 tanchuang弹窗 type类型 zu_ids可看部门 content内容 status状态

        if (empty($data['name'])) {
            apiReturn(201,'标题不能为空');
        }
        if (empty($data['type'])) {
            apiReturn(202,'类型不能为空');
        }
        if (empty($data['content'])) {
            apiReturn(203,'内容不能为空');
        }
        $data['date'] = time();
        if (empty($data['id'])) {
            db('crm_notice')->insert($data);
            apiReturn(200,'新增公告成功');
        }else{
            db('crm_notice')->update($data);
            apiReturn(200,'修改公告成功');
        }
    }

    // 公告列表
    public function noticeList()
    {
        $data = input();//token rows page

        $where = [];

        $noticeList = db('crm_notice')->where($where)->limit(($data['page']-1)*$data['rows'],$data['rows'])->order('date DESC')->select();
        $count = db('crm_notice')->where($where)->count();

        $datas = array(
            'noticeList'=>$noticeList,
            'count'=>$count
        );
        apiReturn(200,'获取公告列表成功',$datas);
    }

    // 公告信息
    public function noticeInfo()
    {
        $data = input();//token id

        $customerInfo = db('crm_notice')->find($data['id']);
        apiReturn(200,'获取公告信息成功',$customerInfo);
    }

    // 删除公告
    public function noticeDelete()
    {
        $data = input();//token id
        if (empty($data['id'])) {
            apiReturn(201,'公告id不能为空');
        }
        db('crm_notice')->where('id',$data['id'])->delete();
        apiReturn(200,'删除公告成功');
    }

}
